package com.wwwarehouse.carddesk.adapter.teamadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;

import java.util.List;

/**
 * @创建者 chenchao.
 * @创建时间 2017/11/16 10:11
 * @描述 ${TODO}
 */

public class DownClassInfoAdapter extends BaseAdapter {
    private Context                                  mContext;
    private List<CardDeskGroupGetGroupBean.ListBean> mDownClassInfoBeanList;
    private int                                      mItemHeight;

    public DownClassInfoAdapter(Context context, List list) {
        mContext = context;
        mDownClassInfoBeanList = list;
        caculateItemHeight();
    }

    private void caculateItemHeight() {
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int screenWidth = ScreenUtils.getScreenWidth(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);
        mItemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext, 120)) / 3;
    }

    @Override
    public int getCount() {
        return mDownClassInfoBeanList == null ? 0 : mDownClassInfoBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDownClassInfoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CardDeskGroupGetGroupBean.ListBean listBean = mDownClassInfoBeanList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_down_class_details, null);
            holder = new ViewHolder();
            holder.civHead = (CircleImageView) convertView.findViewById(R.id.civ_head);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvOnline = (TextView) convertView.findViewById(R.id.tv_online);
//            holder.tvRole = (TextView) convertView.findViewById(R.id.tv_role);
            holder.btRank = (StateButton) convertView.findViewById(R.id.bt_rank);
            holder.btDelay = (StateButton) convertView.findViewById(R.id.bt_delay);
            holder.btDelay.setRadius(new float[]{0, 0, 0, 0, ConvertUtils.dip2px(mContext, 3), ConvertUtils.dip2px(mContext, 3), ConvertUtils.dip2px(mContext, 3), ConvertUtils.dip2px(mContext, 3)});
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mItemHeight;
            convertView.setLayoutParams(params);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BaseApplication.getApplicationInstance().displayImg(listBean.getFaceUrl(), holder.civHead);
        holder.tvName.setText(listBean.getPersonName());
        if (1==listBean.getIsLogin()) {//在线
            holder.tvOnline.setText(mContext.getResources().getString(R.string.teamdesk_group_inline));
        } else {
            holder.tvOnline.setText(mContext.getResources().getString(R.string.teamdesk_group_offline));
        }
        if (listBean.getRanking() != null) {
            holder.btRank.setVisibility(View.VISIBLE);
            holder.btRank.setText(String.format(mContext.getResources().getString(R.string.teamdesk_rank), listBean.getRanking()+""));
        }
        if (1==listBean.getIsDelayed()) {//延时
            holder.btDelay.setVisibility(View.VISIBLE);
        } else {
            holder.btDelay.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        private CircleImageView civHead;
        private TextView        tvName;
        private TextView        tvOnline;
//        private TextView        tvRole;
        private StateButton     btRank;
        private StateButton     btDelay;
    }
}
